package com.travel.sight.web;

import com.alibaba.fastjson.JSON;
import com.travel.common.vo.MemberRespVo;
import com.travel.sight.service.CollectionService;
import com.travel.sight.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

import static com.travel.common.constant.AuthServerConstant.LOGIN_USER;

@Slf4j
@Controller
public class CollectionWebController {

    @Autowired
    CollectionService collectionService;

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 收藏方法
     * @param attractionId
     * @return
     */
    @GetMapping("/saveCollection")
    public String saveCollection(@RequestParam("attractionId")Integer attractionId){
        //1. 从redis中取出用户信息
        String s = redisTemplate.opsForValue().get(LOGIN_USER);
        MemberRespVo memberRespVo = JSON.parseObject(s,MemberRespVo.class);
        Integer userId = memberRespVo.getMemberId().intValue();

        //2. 判断是否收藏过
        Boolean isCollection = collectionService.isCollection(attractionId, userId);

        //3. 更新收藏后的一系列操作
        collectionService.updateAfterCollection(attractionId,userId,isCollection);


        //http://sight.travel.com/43.html
        return "redirect:http://sight.travel.com/"+attractionId+".html";
    }
}
